package com.budwk.app.sqfwV2.generator.caringStaff;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.models.Sqfw_form_summary_pdf;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfPdfGenerator;
import com.budwk.app.sqfwV2.dto.BriefDTO;
import com.budwk.app.sqfwV2.dto.CaringStaffDTO;
import com.budwk.app.sqfwV2.dto.LandscapePatrolDTO;
import com.budwk.app.sqfwV2.dto.dulaying.DuLaYingPropagandaOrScreeningPdfDTO;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import org.nutz.dao.Cnd;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CaringStaffPdfGenerator extends BasePdfGenerator {

    private String templateName;

    private String downName;

    private Sqfw_form_summary_pdf sqfwFormSummaryPdf;

    public Sqfw_form_summary_pdf getSqfwFormSummaryPdf() {
        return sqfwFormSummaryPdf;
    }

    public void setSqfwFormSummaryPdf(Sqfw_form_summary_pdf sqfwFormSummaryPdf) {
        this.sqfwFormSummaryPdf = sqfwFormSummaryPdf;
    }

    public void setDownName(String downName) {
        this.downName = downName;
    }

    public String getDownName() {
        return downName;
    }

    public CaringStaffPdfGenerator(){}




    public CaringStaffPdfGenerator(String templateName){
        this.templateName=templateName;
    }



    public String getTemplateName() {
        return this.templateName;
    }


    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        return false;
    }




    @Override
    protected boolean pdfCreateConditionCheck(PdfGenerateParam pdfGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        CompleteFlowBusinessParam businessParam = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
        CaringStaffDTO caringStaffDTO = (CaringStaffDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        paramMap.put("fillDate", DateUtil.format(caringStaffDTO.getFillDate(), "yyyy年MM月dd日"));
        if("直属".equals( businessParam.getFormSummary().getCommunity())){
            paramMap.put("community",  businessParam.getFormSummary().getCounty());
        }else {
            if(!businessParam.getFormSummary().getCommunity().contains("社区")){
                businessParam.getFormSummary().setCommunity(businessParam.getFormSummary().getCommunity()+"社区");
            }
            paramMap.put("county",  businessParam.getFormSummary().getCounty()+businessParam.getFormSummary().getCommunity());
        }
        List<String>  brief=new ArrayList<>();
        List<BriefDTO> list=  caringStaffDTO.getBriefList();
        for (BriefDTO briefDTO : list) {
            if("是".equals(briefDTO.getWhetherOrNot())){
                brief.add(briefDTO.getProblemContent());
            }
        }
        paramMap.put("brief",CollectionUtil.isEmpty(brief)?"无":String.join("\n",brief));
        if(IdcardUtil.isValidCard(caringStaffDTO.getIDNumber())){
            paramMap.put("age", IdcardUtil.getAgeByIdCard(caringStaffDTO.getIDNumber()));
            paramMap.put("gender", IdcardUtil.getGenderByIdCard(caringStaffDTO.getIDNumber())==1?"男":"女");
        }else if(StrUtil.isNotEmpty(caringStaffDTO.getDateOfBirth())) {
            paramMap.put("age",  DateUtil.ageOfNow(caringStaffDTO.getDateOfBirth()));
            paramMap.put("gender",caringStaffDTO.getGender());
        }

        List<String> careCategoryList=Arrays.asList("空巢老人,独居老人,低保,特困,孤儿,事实无人抚养,残疾人,困难退役军人,其它优抚对象".split(","));

        for (String s : careCategoryList) {
           if( caringStaffDTO.getCareCategory().contains(s)){
               paramMap.put("careCategory"+careCategoryList.indexOf(s), s);
           }else {
               paramMap.put("careCategory"+careCategoryList.indexOf(s), "");
           }
        }
        List<String> strings=Arrays.asList("光荣在党五十年,困难党员,重大贡献党员".split(","));
        List<String> strings1=new ArrayList<>();
        for (String s : strings) {
            if( caringStaffDTO.getCareCategory().contains(s)){
                strings1.add(s);
            }
        }
        paramMap.put("careCategory", String.join(",",strings1));
        // 图片处理
        String images = caringStaffDTO.getVisitPictures();
        String fieldName = ClassUtil.getField(DuLaYingPropagandaOrScreeningPdfDTO::getImages);
        List<String> pictureUrlList = images == null ? Arrays.asList("") : Arrays.asList(images.split(","));
        List<Map<String, PictureRenderData>> pictures = CommonFormOfficeUtils.addWordPicture(pictureUrlList, fieldName, 6.0F);
        paramMap.put(fieldName + "s", pictures);
        paramMap.put("checkedUnitLeader",CommonFormOfficeUtils.addWordPicture(caringStaffDTO.getCheckedUnitLeader(), 2.0F) );
        if("是".equals(caringStaffDTO.getWhetherInsuranceParticipation())){
            paramMap.put("insuranceParticipationType","是，"+caringStaffDTO.getInsuranceParticipationType());
        }else {
            paramMap.put("insuranceParticipationType","否");
        }
        if("是".equals(caringStaffDTO.getWhetherOtherAbnormal())){
            paramMap.put("whetherOtherAbnormal","是，"+caringStaffDTO.getOtherHiddenDangers() );
        }
        if(StrUtil.isNotEmpty(pdfGenerateParam.getCreateUserCommonParam().getCreateUserSigUrl())){
            paramMap.put("createUserSigUrl",CommonFormOfficeUtils.addWordPicture( pdfGenerateParam.getCreateUserCommonParam().getCreateUserSigUrl(), 2.0F) );
            paramMap.put("createUserSigStr",null);
        }else {
            paramMap.put("createUserSigUrl",null);
            paramMap.put("createUserSigStr",pdfGenerateParam.getCreateUserCommonParam().getCreateUserName());
        }


    }


    @Override
    protected String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return this.templateName;
    }


    protected void createXWPDFTemplateAfter(XWPFTemplate template, PdfGenerateParam pdfGenerateParam) {

    }


}
